package com.ksm.zyz.controller;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.sys.entity.BaseGoodsStock;
import com.ksm.zyz.sys.entity.BaseMemberInfo;
import com.ksm.zyz.sys.entity.BaseSupplierInfo;
import com.ksm.zyz.sys.entity.BaseValuecardInfo;
import com.ksm.zyz.sys.entity.OrderInstockGoods;
import com.ksm.zyz.sys.entity.OrderInstockInfo;
import com.ksm.zyz.sys.entity.OrderPurchaseGoods;
import com.ksm.zyz.sys.entity.OrderPurchaseInfo;
import com.ksm.zyz.sys.entity.mySqltongbu;
import com.ksm.zyz.sys.serverentity.Tongbu;
import com.ksm.zyz.sys.service.OrderPurchaseGoodsService;
import com.ksm.zyz.sys.service.OrderPurchaseInfoService;
import com.ksm.zyz.sys.service.baseGoodsStockService;
import com.ksm.zyz.sys.service.baseMemberInfoService;
import com.ksm.zyz.sys.service.baseSupplierInfoService;
import com.ksm.zyz.sys.service.baseValuecardInfoService;
import com.ksm.zyz.sys.service.mySqlTongbuService;
import com.ksm.zyz.sys.service.orderInstockGoodsService;
import com.ksm.zyz.sys.service.orderInstockInfoService;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping("tbtoclient")
public class SyncClientController extends BaseController<Tongbu>{

	@Autowired
	private mySqlTongbuService Service;
	@Autowired
	private baseSupplierInfoService BaseSupplierInfoService;
	@Autowired
	private orderInstockGoodsService OrderInstockGoodsService;
	@Autowired
	private OrderPurchaseGoodsService OrderPurchaseGoodsService;
	@Autowired
	private OrderPurchaseInfoService OrderPurchaseInfoService;
	@Autowired
	private orderInstockInfoService OrderInstockInfoService;
	@Autowired
	private baseGoodsStockService BaseGoodsStockService;
	@Autowired
	private baseMemberInfoService BaseMemberInfoService;
	@Autowired
	private baseValuecardInfoService BaseValuecardInfoService;
	
	 public static final String POST_URL = "http://192.168.124.24:8080/nbzapp/user/userLogin.do?";  

	 @SuppressWarnings("rawtypes")
	@RequestMapping(value ="servertoclient")
	    @ResponseBody
		public String tongbu(HttpServletRequest req){
		 try{
			 //数据包的封装
			 // 分为  添加数据 删除 数据  和修改数据
			 // 例：//map("insert",insertmap<"BaseSupplierInfo",List<BaseSupplierInfo>>)
			 
			 int ming=Integer.parseInt(req.getParameter("ming"));
			 Map<String,List> insertmap=new HashMap<String,List>();
			 Map<String,List> deletemap=new HashMap<String,List>();
			 Map<String,List> updatemap=new HashMap<String,List>();
	//region  list<对象>	 
			 //BaseSupplierInfo
			 List<BaseSupplierInfo> insertBaseSupplierInfolist=new ArrayList<BaseSupplierInfo>();
			 List<BaseSupplierInfo> deleteBaseSupplierInfolist=new ArrayList<BaseSupplierInfo>();
			 List<BaseSupplierInfo> updateBaseSupplierInfolist=new ArrayList<BaseSupplierInfo>();
			 //OrderPurchaseGoods
			 List<OrderPurchaseGoods> insertOrderPurchaseGoodslist=new ArrayList<OrderPurchaseGoods>();
			 List<OrderPurchaseGoods> deleteOrderPurchaseGoodslist=new ArrayList<OrderPurchaseGoods>();
			 List<OrderPurchaseGoods> updateOrderPurchaseGoodslist=new ArrayList<OrderPurchaseGoods>();
			 //OrderPurchaseInfo
			 List<OrderPurchaseInfo> insertOrderPurchaseInfolist=new ArrayList<OrderPurchaseInfo>();
			 List<OrderPurchaseInfo> deleteOrderPurchaseInfolist=new ArrayList<OrderPurchaseInfo>();
			 List<OrderPurchaseInfo> updateOrderPurchaseInfolist=new ArrayList<OrderPurchaseInfo>();
			 //OrderInstockInfo
			 List<OrderInstockInfo> insertOrderInstockInfolist=new ArrayList<OrderInstockInfo>();
			 List<OrderInstockInfo> deleteOrderInstockInfolist=new ArrayList<OrderInstockInfo>();
			 List<OrderInstockInfo> updateOrderInstockInfolist=new ArrayList<OrderInstockInfo>();
			 //OrderInstockGoods
			 List<OrderInstockGoods> insertOrderInstockGoodslist=new ArrayList<OrderInstockGoods>();
			 List<OrderInstockGoods> deleteOrderInstockGoodslist=new ArrayList<OrderInstockGoods>();
			 List<OrderInstockGoods> updateOrderInstockGoodslist=new ArrayList<OrderInstockGoods>();
			 //BaseMemberInfo
			 List<BaseMemberInfo> insertBaseMemberInfolist=new ArrayList<BaseMemberInfo>();
			 List<BaseMemberInfo> deleteBaseMemberInfolist=new ArrayList<BaseMemberInfo>();
			 List<BaseMemberInfo> updateBaseMemberInfolist=new ArrayList<BaseMemberInfo>();
			 //BaseMemberInfo
			 List<BaseValuecardInfo> insertBaseValuecardInfolist=new ArrayList<BaseValuecardInfo>();
			 List<BaseValuecardInfo> deleteBaseValuecardInfolist=new ArrayList<BaseValuecardInfo>();
			 List<BaseValuecardInfo> updateBaseValuecardInfolist=new ArrayList<BaseValuecardInfo>();
			 //BaseGoodsStock
			 List<BaseGoodsStock> insertBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();
			 List<BaseGoodsStock> deleteBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();
			 List<BaseGoodsStock> updateBaseGoodsStocklist=new ArrayList<BaseGoodsStock>();
	   //endregion		 
			 mySqltongbu mstb= new mySqltongbu();
			 mstb.setSmId(ming);
			 List<mySqltongbu> tlist= Service.selectSmIdnew(mstb);
	//region   封装	 
			 int size=tlist.size();
			 for(int i=0;i<size;i++)
			  {
			    String operate=tlist.get(i).getOperate();
				String Biaoming=tlist.get(i).getBiaoming();
				String idkey=tlist.get(i).getIdkey();
								
				if(".".equals(Biaoming))
				{
					
				}				
			 else if("base_supplier_info".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  BaseSupplierInfo info=new BaseSupplierInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);				
					  info.setSmId(ming);
					  BaseSupplierInfo inf=BaseSupplierInfoService.selectInfo(info);
					  inf.setId(null);
					  insertBaseSupplierInfolist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  BaseSupplierInfo info=new BaseSupplierInfo();
					  info.setSupplierCode(idkey);
					  info.setSmId(ming);
					  deleteBaseSupplierInfolist.add(info);
					}
					else if("updated".equals(operate))
					{
					  BaseSupplierInfo info=new BaseSupplierInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  BaseSupplierInfo inf=BaseSupplierInfoService.selectInfo(info);
					  updateBaseSupplierInfolist.add(inf);
					}
				}
			 else if("order_purchase_goods".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  OrderPurchaseGoods info=new OrderPurchaseGoods();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderPurchaseGoods inf=OrderPurchaseGoodsService.selectInfo(info);
					  inf.setId(null);
					  insertOrderPurchaseGoodslist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  OrderPurchaseGoods info=new OrderPurchaseGoods();
					  int id=Integer.parseInt(idkey);
					  info.setId(id);
					  info.setSmId(ming);
					  deleteOrderPurchaseGoodslist.add(info);
					}
					else if("updated".equals(operate))
					{
					  OrderPurchaseGoods info=new OrderPurchaseGoods();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderPurchaseGoods inf=OrderPurchaseGoodsService.selectInfo(info);
					  inf.setId(null);
					  updateOrderPurchaseGoodslist.add(inf);
					}
				}
			 else if("order_purchase_info".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  OrderPurchaseInfo info=new OrderPurchaseInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderPurchaseInfo inf=OrderPurchaseInfoService.selectInfo(info);
					  inf.setId(null);
					  insertOrderPurchaseInfolist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  OrderPurchaseInfo info=new OrderPurchaseInfo();
					  //int id=Integer.parseInt(idkey);
					  info.setOrderNum(idkey);
					  info.setSmId(ming);
					  deleteOrderPurchaseInfolist.add(info);
					}
					else if("updated".equals(operate))
					{
					  OrderPurchaseInfo info=new OrderPurchaseInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderPurchaseInfo inf=OrderPurchaseInfoService.selectInfo(info);
					  inf.setId(null);
					  updateOrderPurchaseInfolist.add(inf);
					}
				}
			 else if("order_instock_info".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  OrderInstockInfo info=new OrderInstockInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderInstockInfo inf=OrderInstockInfoService.selectInfo(info);
					  inf.setId(null);
					  insertOrderInstockInfolist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  OrderInstockInfo info=new OrderInstockInfo();
					  //int id=Integer.parseInt(idkey);
					  info.setOrderNum(idkey);
					  info.setSmId(ming);
					  deleteOrderInstockInfolist.add(info);
					}
					else if("updated".equals(operate))
					{
					  OrderInstockInfo info=new OrderInstockInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderInstockInfo inf=OrderInstockInfoService.selectInfo(info);
					  inf.setId(null);
					  updateOrderInstockInfolist.add(inf);
					}
				}
			 else if("order_instock_goods".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  OrderInstockGoods info=new OrderInstockGoods();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderInstockGoods inf=OrderInstockGoodsService.selectInfo(info);
					  inf.setId(null);
					  insertOrderInstockGoodslist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  OrderInstockGoods info=new OrderInstockGoods();
					  int id=Integer.parseInt(idkey);
					  info.setId(id);
					  info.setSmId(ming);
					  deleteOrderInstockGoodslist.add(info);
					}
					else if("updated".equals(operate))
					{
					  OrderInstockGoods info=new OrderInstockGoods();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  OrderInstockGoods inf=OrderInstockGoodsService.selectInfo(info);
					  inf.setId(null);
					  updateOrderInstockGoodslist.add(inf);
					}
				}
			 
			 else if("base_member_info".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  BaseMemberInfo info=new BaseMemberInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  BaseMemberInfo inf=BaseMemberInfoService.selectInfo(info);
					  inf.setMid(null);
					  insertBaseMemberInfolist.add(inf);
					}
					else if("delete".equals(operate))
					{
						BaseMemberInfo info=new BaseMemberInfo();
					 int id=Integer.parseInt(idkey);
					  info.setMid(id);
					  info.setSmId(ming);
					  deleteBaseMemberInfolist.add(info);
					}
					else if("updated".equals(operate))
					{
						BaseMemberInfo info=new BaseMemberInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  BaseMemberInfo inf=BaseMemberInfoService.selectInfo(info);
					  inf.setMid(null);
					  updateBaseMemberInfolist.add(inf);
					}
				}
			 else if("base_valuecard_info".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  BaseValuecardInfo info=new BaseValuecardInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  BaseValuecardInfo inf=BaseValuecardInfoService.selectInfo(info);
					  inf.setId(null);
					  insertBaseValuecardInfolist.add(inf);
					}
					else if("delete".equals(operate))
					{
						BaseValuecardInfo info=new BaseValuecardInfo();
					 int id=Integer.parseInt(idkey);
					  info.setId(id);
					  info.setSmId(ming);
					  deleteBaseValuecardInfolist.add(info);
					}
					else if("updated".equals(operate))
					{
						BaseValuecardInfo info=new BaseValuecardInfo();
					  int id=Integer.parseInt(idkey);
					  info.setByid(id);	
					  info.setSmId(ming);
					  BaseValuecardInfo inf=BaseValuecardInfoService.selectInfo(info);
					  inf.setId(null);
					  updateBaseValuecardInfolist.add(inf);
					}
				}
			 else if("base_goods_stock".equals(Biaoming))
				{
					if("inserted".equals(operate))
					{ 
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  BaseGoodsStock inf=BaseGoodsStockService.selectInfo(info);
					  inf.setId(null);
					  insertBaseGoodsStocklist.add(inf);
					}
					else if("delete".equals(operate))
					{
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  deleteBaseGoodsStocklist.add(info);
					}
					else if("updated".equals(operate))
					{
					  BaseGoodsStock info=new BaseGoodsStock();
					  //int id=Integer.parseInt(idkey);
					  info.setGoodsNum(idkey);
					  info.setSmId(ming);
					  BaseGoodsStock inf=BaseGoodsStockService.selectInfo(info);
					  inf.setFlag(tlist.get(i).getFlagsu().intValue());
					  inf.setId(null);
					  updateBaseGoodsStocklist.add(inf);
					}
				}
				mySqltongbu mstongbu=tlist.get(i);
				mstongbu.setUstate(1);
				Date now = new Date(); 
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
				String tongbutime = dateFormat.format(now); 
				mstongbu.setTongbutime(tongbutime);
				Service.updateInfo(mstongbu);
						
			  }
			//endregion
			 
	//region   加入map
			 if(insertBaseSupplierInfolist.size()!=0)
			 {
			 insertmap.put("BaseSupplierInfo", insertBaseSupplierInfolist);
			 }
			 if(deleteBaseSupplierInfolist.size()!=0)
			 {
			deletemap.put("BaseSupplierInfo", deleteBaseSupplierInfolist); 
			 }
			 if(updateBaseSupplierInfolist.size()!=0)
			 {
			updatemap.put("BaseSupplierInfo", updateBaseSupplierInfolist); 
			 }
			 
			 if(insertOrderPurchaseGoodslist.size()!=0)
			 {
			 insertmap.put("OrderPurchaseGoods", insertOrderPurchaseGoodslist);
			 }
			 if(deleteOrderPurchaseGoodslist.size()!=0)
			 {
			deletemap.put("OrderPurchaseGoods", deleteOrderPurchaseGoodslist); 
			 }
			 if(updateOrderPurchaseGoodslist.size()!=0)
			 {
			updatemap.put("OrderPurchaseGoods", updateOrderPurchaseGoodslist); 
			 }
			 
			 if(insertOrderPurchaseInfolist.size()!=0)
			 {
			 insertmap.put("OrderPurchaseInfo", insertOrderPurchaseInfolist);
			 }
			 if(deleteOrderPurchaseInfolist.size()!=0)
			 {
			deletemap.put("OrderPurchaseInfo", deleteOrderPurchaseInfolist); 
			 }
			 if(updateOrderPurchaseInfolist.size()!=0)
			 {
			updatemap.put("OrderPurchaseInfo", updateOrderPurchaseInfolist); 
			 }
			 
			 if(insertOrderInstockInfolist.size()!=0)
			 {
			 insertmap.put("OrderInstockInfo", insertOrderInstockInfolist);
			 }
			 if(deleteOrderInstockInfolist.size()!=0)
			 {
			deletemap.put("OrderInstockInfo", deleteOrderInstockInfolist); 
			 }
			 if(updateOrderInstockInfolist.size()!=0)
			 {
			updatemap.put("OrderInstockInfo", updateOrderInstockInfolist); 
			 }
			 
			 if(insertOrderInstockGoodslist.size()!=0)
			 {
			 insertmap.put("OrderInstockGoods", insertOrderInstockGoodslist);
			 }
			 if(deleteOrderInstockGoodslist.size()!=0)
			 {
			deletemap.put("OrderInstockGoods", deleteOrderInstockGoodslist); 
			 }
			 if(updateOrderInstockGoodslist.size()!=0)
			 {
			updatemap.put("OrderInstockGoods", updateOrderInstockGoodslist); 
			 }
			 
			 if(insertBaseMemberInfolist.size()!=0)
			 {
			 insertmap.put("BaseMemberInfo", insertBaseMemberInfolist);
			 }
			 if(deleteBaseMemberInfolist.size()!=0)
			 {
			deletemap.put("BaseMemberInfo", deleteBaseMemberInfolist); 
			 }
			 if(updateBaseMemberInfolist.size()!=0)
			 {
			updatemap.put("BaseMemberInfo", updateBaseMemberInfolist); 
			 } 
			 
			 if(insertBaseValuecardInfolist.size()!=0)
			 {
			 insertmap.put("BaseValuecardInfo", insertBaseValuecardInfolist);
			 }
			 if(deleteBaseValuecardInfolist.size()!=0)
			 {
			deletemap.put("BaseValuecardInfo", deleteBaseValuecardInfolist); 
			 }
			 if(updateBaseValuecardInfolist.size()!=0)
			 {
			updatemap.put("BaseValuecardInfo", updateBaseValuecardInfolist); 
			 }
			 
			 if(insertBaseGoodsStocklist.size()!=0)
			 {
			 insertmap.put("BaseGoodsStock", insertBaseGoodsStocklist);
			 }
			 if(deleteBaseGoodsStocklist.size()!=0)
			 {
			deletemap.put("BaseGoodsStock", deleteBaseGoodsStocklist); 
			 }
			 if(updateBaseGoodsStocklist.size()!=0)
			 {
			updatemap.put("BaseGoodsStock", updateBaseGoodsStocklist); 
			 } 
			//endregion
			 
			 Map<String,Map> map=new HashMap<String, Map>();
			 if(insertmap.size()!=0)
			 map.put("insertmap", insertmap);
			 if(deletemap.size()!=0)
			 map.put("deletemap", deletemap);
			 if(updatemap.size()!=0)
			 map.put("updatemap", updatemap);
			 
			 //判断有没有新的数据
			 if(map==null||map.size()==0)
			 {	
				 System.out.println("没有编号为"+ming+"超市的更新数据");
				 return URLEncoder.encode("no", "utf-8");
			 }
			 else
			 {
			 ObjectMapper mapper=new ObjectMapper();
	         String json1=mapper.writeValueAsString(map);
	         String json=URLEncoder.encode(json1, "utf-8"); //url编码	        
			  return URLEncoder.encode(json, "utf-8");
			 }
		 }
				
		  catch (Exception e) {  
	            e.printStackTrace();  
	            return null;
	        }
		   
	              
		}
  

}
